package com.qdxwx.web.controller.course;

import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.models.channel.Channel;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;

/**
 * Course接口
 * @author developer
 */
@Controller
@RequestMapping("/course")
public class CourseIndexController extends BaseController {

	@Autowired
	private ChannelService channelService;

	@RequestMapping("/index.html")
	public String showCourseIndex(HttpServletRequest request, ModelMap model) {
		return "redirect:/course/list.html";
	}

	@RequestMapping("/{channelDir:[a-zA-Z]+}.html")
	public String showCourseIndex(HttpServletRequest request, @PathVariable("channelDir") String channelDir, ModelMap model) {
		Channel channel = channelService.getByDir(channelDir);
		if (channel == null) {
			return toError404(request);
		}
		//禁用的频道
		if (channel.getIsDisabled() == 1) {
			return toError404(request);
		}
		//判断是否属于模块
		if (channel.getModuleId() != ModuleType.COURSE.ordinal()) {
			return toError404(request);
		}
		return String.format("redirect:/course/%s/list.html", channel.getChannelDir());
		//return getViewPath(String.format("course/%s-index", channel.getChannelDir()));
	}
}